package cn.mxz.robots.basic.remote;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import cn.mxz.robots.pojo.Student;

/**
 * 高考查询省份控制器
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-18
 */
@Component
@Service("examControl")
public class ExamControl extends AbstractExam
{
	@Override
	public String reflectRobots(String provinceId, Student student, String method, Object... params)
	{
		// 返回结果
		String result = null;
		try
		{
			/* 通过反射加载学校机器人，并且调用相应的操作 */
			Class<?> clazz = Class.forName("cn.mxz.robots.province." + ProvinceFactory.chooseProvice(provinceId));// 选择省份

			// 获取类的所有构造函数
			Constructor<?> constructors[] = clazz.getConstructors();
			// 调用其中的含有两个参数的构造函数
			Object univ = constructors[0].newInstance(student);
			// 获取指定名称的方法
			Method univMethod = null;

			int paramsSize = params.length;
			if (paramsSize < 1)
			{// 没有参数
				univMethod = clazz.getMethod(method);
				result = (String) univMethod.invoke(univ);
			}
			else if (paramsSize == 1)
			{// 有一个参数
				univMethod = clazz.getMethod(method, String.class);
				result = (String) univMethod.invoke(univ, params[0].toString());
			}
			else
			{// 有多个参数
				System.out.println("method==>" + method + "\nparams==>f" + Arrays.toString(params));

				univMethod = clazz.getMethod(method, Integer.class, Integer.class, String.class);// 成绩分析功能
				result = String.valueOf(univMethod.invoke(univ, params));
			}

			return result;
		}
		catch (ClassNotFoundException e)
		{
			return "ClassNotFound";
		}
		catch (NoSuchMethodException e)
		{
			return "NoSuchMethod";
		}
		catch (InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
		{
			return "Exception occurs in reflectRotos for 【Exam-reflectRotos】 method  [" + method + "]==>" + e.getMessage() + "==>data:==>" + result;
		}
	}
}
